﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DiaryManager : MonoBehaviour {

	//--Game Objects
	public DiaryData diaryData;
	public DiaryView diaryView;
	public PuzzleRoomManager puzzleRoomManager;
	public CameraManager cameraManager;

	public int currentPage;
	public int currentPage_small;

	// Use this for initialization
	void Awake () {

		//--Sets all GameObjects
		diaryData = GameObject.Find("DiaryManager").GetComponent<DiaryData>();
		diaryView = GameObject.Find("DiaryManager").GetComponent<DiaryView>();
		puzzleRoomManager = GameObject.Find("PuzzleRoomManager").GetComponent<PuzzleRoomManager>();
		cameraManager = GameObject.Find(this.name).GetComponent<CameraManager>();

		diaryData.arrangedDiary = new List<GameObject>();
		diaryData.diaryPages = new List<GameObject>(GameObject.FindGameObjectsWithTag("Diary"));
		diaryData.diaryPages_small = new List<GameObject>(GameObject.FindGameObjectsWithTag("Diary_Small"));
		diaryData.removedDiaryPages = new List<GameObject>();
		diaryData.diaryPageBlank = GameObject.Find("page_");
		diaryData.diaryPageBlank.SetActive(false);
		/*GameObject[] tempDiary = GameObject.FindGameObjectsWithTag("Diary");
		GameObject[] tempDiary_small = GameObject.FindGameObjectsWithTag("Diary_Small");
		List<GameObject> tempDiaryList = new List<GameObject>();
		List<GameObject> tempDiaryList_small = new List<GameObject>();
		for(int i = 0; i < tempDiary.Length; i++)
			tempDiaryList.Add (tempDiary[i]);
		for(int i = 0; i < tempDiary_small.Length; i++)
			tempDiaryList_small.Add(tempDiary_small[i]);
		diaryData.setDiaryPages(tempDiaryList);
		diaryData.setDiaryPages_small(tempDiaryList_small);*/

		for(int i = 0; i < diaryData.diaryPages.Count; i++)
		{
			diaryData.diaryPages[i].SetActive(false);
			diaryData.arrangedDiary.Add(diaryData.diaryPageBlank);
		}
		for(int i = 0; i < diaryData.diaryPages_small.Count; i++)
			diaryData.diaryPages_small[i].SetActive(false);

		currentPage = 0;
		diaryData.arrangedDiary[currentPage].SetActive(true);
		currentPage_small = 0;
		diaryData.diaryPages_small[currentPage_small].SetActive(true);
	}
	
	// Update is called once per frame
	void Update () {
		if(currentPage <= 0)
			diaryView.btnLeft.SetActive(false);
		else
			diaryView.btnLeft.SetActive(true);
		if(currentPage >= diaryData.arrangedDiary.Count - 1)
			diaryView.btnRight.SetActive(false);
		else
			diaryView.btnRight.SetActive(true);
		if(diaryData.arrangedDiary[currentPage] == diaryData.diaryPageBlank)
		{
			diaryView.btnInsert.SetActive(true);
			diaryView.btnRemove.SetActive(false);
		}
		else
		{
			diaryView.btnInsert.SetActive(false);
			diaryView.btnRemove.SetActive(true);
		}
	}

	public void turnLeft(){
		currentPage--;
		diaryData.arrangedDiary[currentPage].SetActive(true);
		diaryView.showedDiaryPage = diaryData.arrangedDiary[currentPage];
		for(int i = 0; i < diaryData.arrangedDiary.Count; i++)
			diaryData.arrangedDiary[i].SetActive(false);
		diaryView.showedDiaryPage.SetActive(true);
	}

	public void turnRight(){
		currentPage++;
		diaryData.arrangedDiary[currentPage].SetActive(true);
		diaryView.showedDiaryPage = diaryData.arrangedDiary[currentPage];
		for(int i = 0; i < diaryData.arrangedDiary.Count; i++)
			diaryData.arrangedDiary[i].SetActive(false);
		diaryView.showedDiaryPage.SetActive(true);
	}

	public void InsertPage()
	{
		for(int i = 0; i < diaryData.diaryPages.Count; i++)
		{
			if(diaryView.showedDiaryPage_small.name.Contains(diaryData.diaryPages[i].name))
			{
				diaryData.arrangedDiary[currentPage] = diaryData.diaryPages[i];
				diaryData.addRemovedDiaryPage(diaryView.showedDiaryPage_small);
				diaryData.diaryPages_small.Remove(diaryView.showedDiaryPage_small);
				diaryView.showedDiaryPage_small.SetActive(false);
				nextDiaryPage();
				return;
			}
		}
	}

	public void RemovePage()
	{
		for(int i = 0; i < diaryData.removedDiaryPages.Count; i++)
		{
			if(diaryData.removedDiaryPages[i].name.Contains(diaryData.arrangedDiary[currentPage].name))
			{
				diaryData.diaryPages_small.Add(diaryData.removedDiaryPages[i]);
				diaryData.removedDiaryPages.Remove(diaryData.removedDiaryPages[i]);
				diaryData.arrangedDiary[currentPage].SetActive(false);
				diaryData.arrangedDiary[currentPage] = diaryData.diaryPageBlank;
				if(diaryData.diaryPages_small.Count >= 0)
				{
					diaryView.showedDiaryPage_small = diaryData.diaryPages_small[0];
					diaryView.showedDiaryPage_small.SetActive(false);
				}
				return;
			}
		}

		/*diaryData.diaryPages_small.Add(diaryData.removedDiaryPages.Find(x => x.name.Contains(diaryData.arrangedDiary[currentPage].name)));
		diaryData.removedDiaryPages.Remove(diaryData.diaryPages_small[diaryData.diaryPages_small.Count - 1]);*/
		/*for(int i = 0; i < diaryData.diaryPages.Count; i++)
			if(diaryView.showedDiaryPage.name == diaryData.diaryPages[i].name)
			{
				diaryData.arrangedDiary[currentPage] = diaryData.diaryPageBlank;
				for(int j = 0; j < diaryData.removedDiaryPages.Count; j++)
				{
					if(diaryData.removedDiaryPages[j].name.Contains(diaryData.diaryPages[i].name))
					{
						diaryData.diaryPages_small.Add(diaryData.removedDiaryPages[j]);
						diaryData.removedDiaryPages.Remove(diaryData.removedDiaryPages[j]);
					}
				}
			diaryView.showedDiaryPage.SetActive(false);
			return;
			}*/
	}

	public void nextDiaryPage()
	{
		if(currentPage_small >= diaryData.diaryPages_small.Count - 1)
			currentPage_small = 0;
		else
			currentPage_small++;
		diaryData.diaryPages_small[currentPage_small].SetActive(true);
		diaryView.showedDiaryPage_small = diaryData.diaryPages_small[currentPage_small];
		for(int i = 0; i < diaryData.getDiaryPages_small().Count; i++)
			diaryData.getDiaryPages_small()[i].SetActive(false);
		diaryView.showedDiaryPage_small.SetActive(true);
	}

	public void ReturnToLevel()
	{
		cameraManager.changeCamera(0);
		puzzleRoomManager.mouseActive = false;
		//cameraManager.changeCamera(0);
	}
}
